package com.huluobo.a2109kotlinshopping.mine

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2109kotlinshopping.R
import com.huluobo.a2109kotlinshopping.base.BaseMVVMActivity
import com.huluobo.a2109kotlinshopping.databinding.ActivityAddressBinding
import com.huluobo.a2109kotlinshopping.db.DatabaseUtil
import com.huluobo.a2109kotlinshopping.net.Const

class AddressActivity : BaseMVVMActivity<ActivityAddressBinding>() {
    private lateinit var viewModel: MineViewModel
    override fun initData() {
        viewModel = ViewModelProvider(this)[MineViewModel::class.java]

        viewModel.updateAddressSuccess.observe(this) {
            if (it.code == 200) {
                ToastUtils.showLong(it.message)
                dataBinding.addressTv.text = it.data
                dataBinding.addressEdit.text.clear()
            }
        }
    }

    override fun initView() {
        dataBinding.addressHeader.headerTitle.text = "收货地址"
        dataBinding.addressHeader.back.setOnClickListener {
            finish()
        }
        val queryUserInfo = DatabaseUtil.getDb().userInfoDao().queryUserInfo(SPUtils.getInstance().getInt(Const.SP_ID))
        dataBinding.addressTv.text = queryUserInfo.address

        dataBinding.addressCommit.setOnClickListener {
            if (dataBinding.addressEdit.text.toString().isNotEmpty()) {
                viewModel.updateAddress(dataBinding.addressEdit.text.toString())
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_address
}